﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace alphabook
{
    public partial class Main : Form

    {
        Book selectedBook;
        User selectedUser;
        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            this.fillBooks();
            this.fillBooksOut();
            this.fillUsers();
            this.fillReturnBooks();
        }

        private void fillBooks()
        {
            book_list.Items.Clear();
            Book[] books = Book.getAllBooks();
            foreach(Book b in books)
            {
                book_list.Items.Add(b.Title + " - " + b.Author);
            }
        }

        private void add_button_Click(object sender, EventArgs e)
        {

            bool pass = false;
            if (book_title.Text.Length == 0)
            {
                book_title_label.ForeColor = Color.Red;
                pass = false;
            }
            else
            {
                book_title_label.ForeColor = Color.Black;
                pass = true;
            }
            if(book_author.Text.Length == 0)
            {
                book_author_label.ForeColor = Color.Red;
                pass = false;
            }
            else
            {
                book_author_label.ForeColor = Color.Black;
                pass = true;
            }
            if (!pass)
                return;

            int year;
            if (int.TryParse(book_year.Text, out year))
            {
                year = int.Parse(book_year.Text);
            }

            if (selectedBook != null)
            {
                book_list.Items.Remove(book_list.SelectedItem);
                selectedBook.Title = book_title.Text;
                selectedBook.Author = book_author.Text;
                selectedBook.Year = year;
                book_debug_label.ForeColor = Color.Green;
                book_debug_label.Text = "Raamat on muudetud!";
                book_list.Items.Add(selectedBook.Title + " - " + selectedBook.Author);
                selectedBook = null;
                add_button.Text = "Lisa";
                return;
            }
            
            Book addBook = new Book(book_title.Text, year, book_author.Text);

            if (addBook.Title == "")
            {
                book_debug_label.ForeColor = Color.Red;
                book_debug_label.Text = "Sellise nimega raamat on andmebaasis olemas!";
            }
            else
            {
                book_debug_label.ForeColor = Color.Green;
                book_debug_label.Text = "Raamat on lisatud!";
                book_title.Text = "";
                book_author.Text = "";
                book_year.Text = "";
                book_list.Items.Add(addBook.Title + " - " + addBook.Author);
            }
        }

        private void book_list_SelectedIndexChanged(object sender, EventArgs e)
        {
            book_title_label.ForeColor = Color.Black;
            book_author_label.ForeColor = Color.Black;
            book_debug_label.Text = "";
            book_cancel.Enabled = true;

            if (book_list.SelectedItem == null)
                return;
            add_button.Text = "Muuda";
            char[] splitter = {'-'};
            string[] splitted = book_list.SelectedItem.ToString().Split(splitter);
            selectedBook = Book.getBook(splitted[0]);
            book_title.Text = splitted[0];
            book_author.Text = selectedBook.Author;
            book_year.Text = selectedBook.Year.ToString();
        }

        private void fillUsers()
        {
            user_list.Items.Clear();
            users_out.Items.Clear();
            User[] users = User.getAllUsers();
            foreach (User u in users)
            {
                user_list.Items.Add(u.Username);
                users_out.Items.Add(u.Username);
            }
        }

        private void add_user_Click(object sender, EventArgs e)
        {
            username_label.ForeColor = Color.Black;
            first_name.ForeColor = Color.Black;
            bool pass = false;
            if (username.Text.Length == 0)
            {
                username_label.ForeColor = Color.Red;
                pass = false;
            }
            else
            {
                username_label.ForeColor = Color.Black;
                pass = true;
            }
            if (first_name.Text.Length == 0)
            {
                first_name_label.ForeColor = Color.Red;
                pass = false;
            }
            else
            {
                first_name.ForeColor = Color.Black;
                pass = true;
            }
            if (!pass)
                return;

            if (selectedUser != null)
            {
                user_list.Items.Remove(selectedUser.Username);
                selectedUser.Username = username.Text;
                selectedUser.Firstname = first_name.Text;
                selectedUser.Surname = surname.Text;
                user_list.Items.Add(username.Text);
                user_debug_label.ForeColor = Color.Green;
                user_debug_label.Text = "Kasutaja muudetud!";
                username.Text = "";
                first_name.Text = "";
                surname.Text = "";
                users_cancel.Enabled = false;
                selectedUser = null;
                return;
            }

            User addUser = new User(username.Text, first_name.Text, surname.Text);
            if (addUser.Username == "")
            {
                user_debug_label.ForeColor = Color.Red;
                user_debug_label.Text = "Selline kasutaja eksisteerib juba!";
            }
            else
            {
                user_debug_label.ForeColor = Color.Green;
                user_debug_label.Text = "Kasutaja lisatud!";
                user_list.Items.Add(username.Text);
                username.Text = "";
                first_name.Text = "";
                surname.Text = "";
            }

        }
        private void user_list_SelectedIndexChanged(object sender, EventArgs e)
        {
            username_label.ForeColor = Color.Black;
            first_name_label.ForeColor = Color.Black;
            book_debug_label.Text = "";

            if (user_list.SelectedItem == null)
                return;
            add_user_button.Text = "Muuda";
            selectedUser = User.getUser(user_list.SelectedItem.ToString());
            username.Text = selectedUser.Username;
            first_name.Text = selectedUser.Firstname;
            surname.Text = selectedUser.Surname;
            users_cancel.Enabled = true;
        }

        private void fillBooksOut(){
            books_out.Items.Clear();
            Book[] books = Book.getPresentBooks();
            foreach (Book b in books)
            {
                books_out.Items.Add(b.Title + " - " + b.Author);
            }
        }

        private void lend_button_Click(object sender, EventArgs e)
        {
            if (users_out.SelectedItem == null || books_out.SelectedItem == null)
                return;
            String user = users_out.SelectedItem.ToString();
            String book = books_out.SelectedItem.ToString();
            //char[] splitter = {'-'};
            //string[] splitted = book_list.SelectedItem.ToString().Split(splitter);
            string[] splitted = book.Split('-');
            User lender = User.getUser(user);
            Book bookOut = Book.getBook(splitted[0]);
            Loan addLoan = new Loan(lender, bookOut, lend_date.Value);
            books_out.Items.Remove(book);
            int n = return_grid.Rows.Add();
            return_grid.Rows[n].Cells[0].Value = book;
            return_grid.Rows[n].Cells[1].Value = user;
            return_grid.Rows[n].Cells[2].Value = lend_date.Value.ToString();
            lend_debug_label.ForeColor = Color.Green;
            lend_debug_label.Text = "Laenutus on lisatud!";
        }

        private void book_cancel_Click(object sender, EventArgs e)
        {
            book_cancel.Enabled = false;
            add_button.Text = "Lisa";
            selectedBook = null;
            book_title.Text = "";
            book_author.Text = "";
            book_year.Text = "";
        }

        private void return_button_Click(object sender, EventArgs e)
        {
            int row = return_grid.CurrentCell.RowIndex;
            String bookName = return_grid.Rows[row].Cells[0].Value.ToString();
            String userName = return_grid.Rows[row].Cells[1].Value.ToString();
            string[] splitted = bookName.Split('-');
            Book b = Book.getBook(splitted[0]);
            User u = User.getUser(userName);
            Loan.ReturnBook(b, u);
            books_out.Items.Add(bookName);
            return_grid.Rows.RemoveAt(row);
        }

        private void fillReturnBooks()
        {
            Loan[] loans = Loan.GetActiveLoans();
            foreach (Loan l in loans)
            {
                int n = return_grid.Rows.Add();
                return_grid.Rows[n].Cells[0].Value = l.LentBook.Title + " - " + l.LentBook.Author;
                return_grid.Rows[n].Cells[1].Value = l.Lender.Username;
                return_grid.Rows[n].Cells[2].Value = l.ExpectedIn;
            }
        }

        private void Katkesta_Click(object sender, EventArgs e)
        {
            users_cancel.Enabled = false;
            add_user_button.Text = "Lisa";
            selectedUser = null;
            username.Text = "";
            first_name.Text = "";
            surname.Text = "";
        }

        private void report_button_Click(object sender, EventArgs e)
        {
            report_grid.Columns.Clear();
            Book[] books;
            Loan[] loans;
            switch (report_select.SelectedIndex)
            {
                case 0:
                    //Kodus olevad raamatud
                    books = Book.getPresentBooks();
                    report_grid.Columns.Add("Raamat", "Raamat");
                    report_grid.Columns.Add("Autor", "Autor");
                    report_grid.Columns.Add("Aasta", "Aasta");
                    foreach (Book b in books)
                    {
                        int n = report_grid.Rows.Add();
                        report_grid.Rows[n].Cells[0].Value = b.Title;
                        report_grid.Rows[n].Cells[1].Value = b.Author;
                        report_grid.Rows[n].Cells[2].Value = b.Year;
                    }
                break;
                case 1:
                    //Välja laenutatud raamatud
                    loans = Loan.GetActiveLoans();
                    report_grid.Columns.Add("Raamat", "Raamat");
                    report_grid.Columns.Add("Autor", "Autor");
                    report_grid.Columns.Add("Aasta", "Aasta");
                    report_grid.Columns.Add("Kasutajanimi", "Kasutajanimi");
                    report_grid.Columns.Add("Välja laenutatud", "Välja laenutatud");
                    report_grid.Columns.Add("Tähtaeg", "Tähtaeg");
                    foreach (Loan l in loans)
                    {
                        int n = report_grid.Rows.Add();
                        report_grid.Rows[n].Cells[0].Value = l.LentBook.Title;
                        report_grid.Rows[n].Cells[1].Value = l.LentBook.Author;
                        report_grid.Rows[n].Cells[2].Value = l.LentBook.Year;
                        report_grid.Rows[n].Cells[3].Value = l.Lender.Username;
                        report_grid.Rows[n].Cells[4].Value = l.OutDate;
                        report_grid.Rows[n].Cells[5].Value = l.ExpectedIn;
                    }
                break;
                case 2:
                    //Tähtaja ületanud raamatud
                    loans = Loan.GetOverdueLoans();
                    report_grid.Columns.Add("Raamat", "Raamat");
                    report_grid.Columns.Add("Autor", "Autor");
                    report_grid.Columns.Add("Aasta", "Aasta");
                    report_grid.Columns.Add("Kasutajanimi", "Kasutajanimi");
                    report_grid.Columns.Add("Välja laenutatud", "Välja laenutatud");
                    report_grid.Columns.Add("Tähtaeg", "Tähtaeg");
                    foreach (Loan l in loans)
                    {
                        int n = report_grid.Rows.Add();
                        report_grid.Rows[n].Cells[0].Value = l.LentBook.Title;
                        report_grid.Rows[n].Cells[1].Value = l.LentBook.Author;
                        report_grid.Rows[n].Cells[2].Value = l.LentBook.Year;
                        report_grid.Rows[n].Cells[3].Value = l.Lender.Username;
                        report_grid.Rows[n].Cells[4].Value = l.OutDate;
                        report_grid.Rows[n].Cells[5].Value = l.ExpectedIn;
                    }
                break;
            }
        }

        private void tab_change(object sender, EventArgs e)
        {
            if (tabs.SelectedIndex == 1)
            {
                this.fillBooksOut();
                this.fillUsers();
            }
        }
    }
}
